<?php
namespace app\manager\controller;

use app\admin\model\Admins;
use think\File;
use think\Paginator;


Class Index extends Base
{
    Public function index()
    {

        $post = $this->request->post();
        $time = strtotime(date('Y-m-d'));

        $res = !isset($post['key']) ?  Admins::table('wolive_business')->paginate(8) : Admins::table('wolive_business')->where('business_id', 'like', '%' . $post['key'] . '%')->paginate(8);


        $data = [];

        foreach ($res as $vg) {
            $v = $vg['business_id'];
            $cdata = [];
            // 正在谈话人数
            $talknum = Admins::table('wolive_queue')->where(['business_id' => $v])->where('state', 'normal')->where("service_id", '<>', 0)->count();
            // 在线客服人数
            $services = Admins::table("wolive_service")->where(['business_id' => $v, 'state' => 'online'])->count();
            // 正在排队人数
            $waitnum = Admins::table("wolive_queue")->where(['business_id' => $v])->where("service_id", 0)->count();
            // 接入总人数
            $totalvisit = Admins::table("wolive_chats")->distinct(true)->field('visiter_id')->where('business_id', $v)->count();
            // 今日会话量
            $nowchat = Admins::table("wolive_chats")->where('business_id', $v)->where('timestamp', '>', "{$time}")->count();
            // 会话总量
            $totalchat = Admins::table('wolive_chats')->where('business_id', $v)->count();
            // 今日留言量
            $nowmsg = Admins::table('wolive_message')->where('business_id', $v)->where('timestamp', 'like', "{time}%")->count();
            // 留言总量
            $totalmsg = Admins::table('wolive_message')->where('business_id', $v)->count();

            $cdata['state'] = $vg['state'];
            $cdata['web'] = $v;
            $cdata['talk'] = $talknum;
            $cdata['service'] = $services;
            $cdata['wait'] = $waitnum;
            $cdata['visited'] = $totalvisit;
            $cdata['nowchat'] = $nowchat;
            $cdata['totalchat'] = $totalchat;
            $cdata['nowmsg'] = $nowmsg;
            $cdata['totalmsg'] = $totalmsg;
            $cdata['exp_timestamp'] = date('Y-m-d', $vg['exp_timestamp'] ? $vg['exp_timestamp'] : $vg['regist_timestamp']);
            $data[] = $cdata;
        }

        $page = $res->render();

        $this->assign("data", $data);
        $this->assign('page', $page);

        $this->assign('title', '首页');


        return $this->fetch();
    }
}